package com.yijia.work4.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class Question3Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        //Math.sqrt(n);//求平方根
        //Math.pow(2, 3);//2的3此房
        //Math.abs(-4);//绝对值
        //Math.log10(100);//...

        if (request.getParameter("num") == "" || request.getParameter("num") == null) {
            session.setAttribute("error", "请输入一个正实数！");
            request.getRequestDispatcher("/work4/3.jsp").forward(request, response);
        } else {
            //用户登录表单的参数
            int num = Integer.valueOf(request.getParameter("num"));

            //计算这个数的平方根
            double result = Math.sqrt(num);

            session.setAttribute("result", result);
            session.setAttribute("num", num);
            request.getRequestDispatcher("/work4/3.jsp").forward(request, response);
        }


    }
}
